package com.ibl.rover.lite.client.Activity;

import com.ibl.rover.lite.client.R;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

/**
 * The code in this class demonstrates the use of an {@link ArrayAdapter} in a
 * {@link ListView} with a set filter. This code is provided as an example of
 * using a filter in a {@link ListView} for the tutorial posted at the follow
 * URL:
 * 
 * @see http://kahdev.wordpress.com/2010/05/22/filtering-a-listview-using-an-
 *      arrayadapter
 * 
 *      Icons in res/drawable-??? directories are from the Open Icon Library
 * 
 * @see http://openiconlibrary.sourceforge.net/
 * 
 * @author Kah
 * 
 */
public class Demonstrate extends ListActivity
{
    private static final int ADD_ITEM = 0;
    private static final int REMOVE_ITEM = 1;
    private static final int FILTER = 2;
    private static final int EXIT_ITEM = 3;

    private ArrayAdapter<Item> dataAdapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        Context context = getApplicationContext();
        dataAdapter = new DataSource(context, R.layout.item, R.id.product_code);
        dataAdapter.add(new Item("Apple", "0.99",""));
        dataAdapter.add(new Item("Orange", "1.50",""));
        dataAdapter.add(new Item("Tomato", "1.00",""));
        
        setListAdapter(dataAdapter);
        getListView().setTextFilterEnabled(true);
    }
 

//    @Override
//    public boolean onOptionsItemSelected(MenuItem item)
//    {
//        Log.i("[ITEM SELECTED]", "Item " + item.getItemId() + " selected");
//        switch (item.getItemId())
//        {
//        case ADD_ITEM:
//            showDialog(ADD_ITEM);
//            break;
//        case REMOVE_ITEM:
//            dataAdapter.remove(dataAdapter.getItem(dataAdapter.getCount() - 1));
//            break;
//        case FILTER:
//            Toast message = Toast.makeText(this, R.string.FILTER_ON,
//                Toast.LENGTH_SHORT);
//            message.show();
//            getListView().setTextFilterEnabled(true);
//
//            break;
//        case EXIT_ITEM:
//            finish();
//        }
//        return true;
//    }

//    @Override
//    protected Dialog onCreateDialog(int id)
//    {
//        AlertDialog.Builder builder = new AlertDialog.Builder(this);
//        builder.setTitle(R.string.addDialogTitle);
//
//        View content = getLayoutInflater().inflate(R.layout.item_editor,
//            (ViewGroup) findViewById(R.id.editLayout));
//        builder.setView(content);
//        builder.setPositiveButton(R.string.addButtonLabel,
//            new DialogInterface.OnClickListener()
//            {
//
//                public void onClick(DialogInterface dialog, int which)
//                {
//                    Dialog source = (Dialog) dialog;
//                    EditText nameField = (EditText) source
//                        .findViewById(R.id.itemField);
//                    EditText priceField = (EditText) source
//                        .findViewById(R.id.priceField);
//                    final String name = nameField.getText().toString();
//                    final Double price = Double.valueOf(priceField.getText()
//                        .toString());
//
//                    EditText timesField = (EditText) source
//                        .findViewById(R.id.timesField);
//                    final Integer times = Integer.valueOf(timesField.getText()
//                        .toString());
//
//                    if ((name.length() > 0) && (times > 0))
//                    {
//                        for (int count = 0; count < times; count++)
//                        {
//                            dataAdapter.add(new Item(name, price));
//                        }
//                    }
//
//                    dialog.dismiss();
//                }
//            });
//
//        builder.setNegativeButton(R.string.cancelLabel,
//            new DialogInterface.OnClickListener()
//            {
//
//                public void onClick(DialogInterface dialog, int which)
//                {
//                    dialog.dismiss();
//                }
//            });
//
//        return builder.create();
//    }
}